﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;

using Common;

namespace DAL
{
    public class SchemaHeler
    {
        public DbProviderFactory DBFactory;
        private string m_ProviderName;
        private string m_ConnectionString;

        public SchemaHeler(string providerName, string connectionString)
        {
            this.m_ProviderName = providerName;
            this.m_ConnectionString = connectionString;
            this.DBFactory = DbProviderFactories.GetFactory(providerName);
        }

        public DataTable GetTables()
        {
            DbConnection conn = null;
            DataTable schema = null;
            try
            {
                conn = DBFactory.CreateConnection();
                conn.ConnectionString = m_ConnectionString;
                conn.Open();
                schema = conn.GetSchema("Tables");
            }
            finally
            {
                if (conn != null && conn.State == System.Data.ConnectionState.Open)
                    conn.Close();
            }
            return schema;
        }

        public DataTable GetTables(string[] tableRestriction)
        {
            DbConnection conn = null;
            DataTable schema = null;
            try
            {
                conn = DBFactory.CreateConnection();
                conn.ConnectionString = m_ConnectionString;
                conn.Open();
                schema = conn.GetSchema("Tables", tableRestriction);
            }
            finally
            {
                if (conn != null && conn.State == System.Data.ConnectionState.Open)
                    conn.Close();
            }
            return schema;
        }

        public DataTable GetSchemaTable(string tableName, string tableType)
        {
            DbDataReader dr = null;
            DataTable schema = null;
            try
            {
                DbCommand cmd = DBFactory.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = string.Format("Select * from {0}", tableName);
                DbConnection conn = DBFactory.CreateConnection();
                conn.ConnectionString = m_ConnectionString;
                cmd.Connection = conn;

                conn.Open();
                if (tableType == TablePropName.TABLE_TYPE__BASE_TABLE)
                {
                    dr = cmd.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.CloseConnection);
                }
                else if (tableType == TablePropName.TABLE_TYPE__VIEW)
                {
                    dr = cmd.ExecuteReader(CommandBehavior.SchemaOnly | CommandBehavior.CloseConnection);
                }
                schema = dr.GetSchemaTable();
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                    dr.Dispose();
                }
            }
            return schema;
        }
    }
}
